//
//  MyActivityViewController.m
//  newDouban
//
//  Created by dllo on 15/10/31.
//  Copyright © 2015年 蓝鸥科技. All rights reserved.
//

#import "MyActivityViewController.h"
#import "ActivityDetailsViewController.h"
#import "SingleTon.h"
#import "DataBaseSingTon.h"

@interface MyActivityViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) UITableView *tableV;
@property (nonatomic, retain) NSMutableArray *arrM;

@end

@implementation MyActivityViewController

- (void)dealloc
{
    [_tableV release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"我的活动";
    self.tableV = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStyleGrouped];
    //self.tableV.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableV registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    self.tableV.delegate = self;
    self.tableV.dataSource = self;
    
    [self.view addSubview:self.tableV];
    [_tableV release];
    
    NSString *isLogin = [[NSUserDefaults standardUserDefaults] objectForKey:@"userName1"];
     //NSString *sql = @"SELECT * FROM activityStore";
    NSString *sql = [NSString stringWithFormat:@"SELECT * FROM activityStore WHERE userName = '%@'", isLogin];
    
    self.arrM = [[DataBaseSingTon shareDataBase] selectActivityStoreSQL:sql];
    
    self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(leftButtion:)] autorelease];
    
}

- (void)leftButtion:(UIBarButtonItem *)sender
{
     UIViewController *VC = [self.navigationController.viewControllers  objectAtIndex:0];
    
    [self.navigationController popToViewController:VC animated:YES];
    
}


- (void)addActivity:(NSString *)title
{
//    NSLog(@"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
//    NSLog(@"title ================== %@", title);
//    [self.arrM addObject:title];
//    [self.tableV reloadData];
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    ActivityStore *activity = [self.arrM objectAtIndex:indexPath.row];
    NSString *sql = [NSString stringWithFormat:@"SELECT * FROM activity WHERE title = '%@'", activity.title];
    NSMutableArray *arrM = [[DataBaseSingTon shareDataBase] selectActivity:sql];
    ActivityDetailsViewController *activityDetailVC = [[ActivityDetailsViewController alloc] init];
    activityDetailVC.activity = [arrM lastObject];
    [self.navigationController pushViewController:activityDetailVC animated:YES];
    [activityDetailVC release];
    
    NSLog(@"indexPath.row  = %ld, indexPath.section  = %ld", indexPath.row, indexPath.section);
    
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.arrM.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//    UITableViewCell *cell = [[MovieListTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
    static NSString *cellIndentifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifier];
    ActivityStore *activity = [self.arrM objectAtIndex:indexPath.row];
    cell.textLabel.text = activity.title;
    
    return cell;
}


- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];
    //设置tableview编辑状态,随按钮状态改变而改变
    [self.tableV setEditing:editing animated:animated];
    
}


- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (UITableViewCellEditingStyleDelete == editingStyle) {
        
        //[self.arrM removeObjectAtIndex:indexPath.row];
        
        //if (self.arrM.count != 0) {
           ActivityStore *activity = [self.arrM objectAtIndex:indexPath.row];
            NSString *sql = [NSString stringWithFormat:@"DELETE FROM activityStore WHERE title = '%@' AND userName = '%@'", activity.title, activity.userName];
            [[DataBaseSingTon shareDataBase] deleteActivity:sql];
        //}
        [self.arrM removeObjectAtIndex:indexPath.row];
        [self.tableV reloadData];
    }
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
